#ifndef	PROC_MANAGER_HPP
# define	PROC_MANAGER_HPP

# include	<unistd.h>
# include	<sys/types.h>
# include	<sys/wait.h>

# include	"Exception.hpp"

class ProcManager
{
public:
  ProcManager() {}
  ~ProcManager() {}

  bool		duplicate(void) {
    if (!this->create_child_process())
      throw Tools::Exception("Error: Cannot create child process");
    return ((this->isChild()) ? (true) : (false));
  }

  ProcManager const&	wait_child() const {
    int	status = 0;

    return (this->wait_child_process(&status));
  }

  ProcManager const&	wait_child_process(int *status) const {
    if (wait(status) == -1)
      throw Tools::Exception("Error: Cannot wait proccess");
    return (*this);
  }

  bool		isChild() const {
    return ((!this->_pid) ? (true) : (false));
  }

  bool		isParent() const {
    return ((this->_pid > 0) ? (true) : (false));
  }

  bool		operator==(ProcManager const& pm) const {
    return ((this->_pid == pm._pid) ? (true) : (false));
  }

private:
  bool		create_child_process(void) {
    return (((this->_pid = fork() != -1) ? (true) : (false)));
  }

private:
  pid_t		_pid;

private:
  ProcManager(ProcManager const&) {}
  ProcManager	&operator=(ProcManager const&);
};

#endif